/*
全局类，主要用来存取本地数据
*/
window.localSetting = {
    loginData : {},
    gameData : {},
    roomData : {},
    //初始化本地数据，先取看看有没有，没有则新建默认、
    initData:function(){
        //登录相关的数据
        if(cc.sys.localStorage.getItem('loginData') == null)
            local.resetLoginData();
        else
            local.loginData = JSON.parse(cc.sys.localStorage.getItem('loginData'));
            
        //游戏相关的数据
        if(cc.sys.localStorage.getItem('gameData') == null)
            local.resetGameData();
        else
            local.gameData = JSON.parse(cc.sys.localStorage.getItem('gameData'));
            if(local.gameData.gameBgType == null)
            {
                local.gameData.gameBgType = 1
                local.saveGameData()
            }
        //房间信息相关的数据
        if(cc.sys.localStorage.getItem('roomData') == null)
            local.resetRoomData();
        else
            local.roomData = JSON.parse(cc.sys.localStorage.getItem('roomData'));
        
        if(cc.sys.localStorage.getItem('roomHistory') == null)
            local.resetRoomHistory();
        else
            local.roomHistory = JSON.parse(cc.sys.localStorage.getItem('roomHistory'));

        local.roomHistoryCount = cc.sys.localStorage.getItem('roomHistoryCount');
        if(local.roomHistory == null)
            local.resetRoomHistory();
        console.log(local.loginData);
        console.log(local.gameData);
        console.log(local.roomData);
        console.log(local.roomHistory);
    },

    //保存本地数据
    saveLoginData:function(){
        cc.sys.localStorage.setItem('loginData', JSON.stringify(local.loginData));
    },
    saveGameData:function(){
        cc.sys.localStorage.setItem('gameData', JSON.stringify(local.gameData));
    },
    saveRoomData:function(){
        cc.sys.localStorage.setItem('roomData', JSON.stringify(local.roomData));
    },
    //重置本地数据
    resetLoginData:function(){
        local.loginData = {
            wxLastLoginDay : null,
            wxRefreshToken : null
        };
        local.saveLoginData();
    },
    resetGameData:function(){
        local.gameData = {
            lastNoticeData : null,
            musicValue : 1,
            soundValue : 1,
            musicEnable : true,
            soundEnable : true,
            gameBgType : 1,
        };
        local.saveGameData();
    },
    resetRoomData:function(){
        local.roomData = {
            musicEnable : true,
            soundEnable : true
        };
        local.saveRoomData();
    },
    //重置俱乐部列表
    resetRoomHistory:function(){
        local.roomHistory = {};
        local.roomHistoryCount = 0;
        local.saveRoomHistory();
    },
    //保存俱乐部列表
    saveRoomHistory:function(){
        cc.sys.localStorage.setItem('roomHistory', JSON.stringify(local.roomHistory));
        cc.sys.localStorage.setItem('roomHistoryCount', local.roomHistoryCount);
    },
    //俱乐部列表插入新记录
    addOneRoomHistory:function(newRoomData){
        for(var i in local.roomHistory)
            if(local.roomHistory[i].roomID == newRoomData.roomID)
            {
                local.roomHistory[i] = newRoomData;
                local.saveRoomHistory();
                return;
            }
        local.roomHistory[local.roomHistoryCount] = newRoomData;
        local.roomHistoryCount++;
        local.saveRoomHistory();
    },
};

var local = window.localSetting;